<?php
/**
 * Product templates model. Product template it is acctually some kind of product type 
 * (i.e. books, tvsets etc). Each template has a set of attributes.  
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 *
 */
class Models_ProductTemplates extends Bel_Model_Abstract  {

	/**
	 * Array of tables' classes which are in one-to-many relation with product templates table.  
	 *
	 * @var array
	 */	
	protected $_dependentTables = array('Models_ProductTemplates_Attributes','Models_Products');
	
	/**
	 * Sets products table name and primary key
	 *
	 * @return void
	 */	
	public function __construct() {
		$this->_name = TABLE_PREFIX . "product_templates";
		$this->_primary = array('product_template_id');
		parent::__construct ();
	}
	
	/**
	 * Return attributes which assigned to template
	 *
	 * @param int $template_id - template id
	 * @return array
	 */
	public function getAttributesByTemplate($template_id){
		$template = $this->find($template_id)->current();
		if($template){
			$oTemplAttr = new Models_ProductTemplates_Attributes();
			
			$attributes = $template->findManyToManyRowset('Models_ProductAttributes',$oTemplAttr,null,null,$oTemplAttr->select()->order('attribute_order'));
			
			$result = array();
			$i=0;
			$oAttrVals = new Models_ProductAttributes_DefaultValues();
			
			foreach ($attributes as $attribute){
				$result[$i] = $attribute->toArray();
				
				
				if($attribute->attribute_field_type=='select' || $attribute->attribute_field_type=='radio'){
					$values = $oAttrVals->getValuesByAttribute($attribute->attribute_id);
					if($values){
						$result[$i]['options'] = $values->toArray();
					}
				}
				$i++;
			}
			return $result;
		}else{
			return false;
		}	
	}
}
